package com.spx.chapter05.source;

import com.spx.chapter05.pojo.Event;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.ArrayList;
import java.util.List;

/**
 * create by undeRdoG on  2022-04-29  14:24
 * 凡心所向，素履以往，生如逆旅，一苇以航。
 */
public class SourceTest {

    public static void main(String[] args) throws Exception {
        // 创建执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        // 从文件中读取
        DataStreamSource<String> stream1 = env.readTextFile("input/clicks.txt");
        stream1.print();

        // 从集合中读取数据
        List<Integer> nums = new ArrayList<>();
        nums.add(2);
        nums.add(5);
        DataStreamSource<Integer> numStream = env.fromCollection(nums);

        List<Event> events = new ArrayList<>();
        events.add(new Event("spx", "./cart", 1000L));
        DataStreamSource<Event> eventStream = env.fromCollection(events);
        eventStream.print("eventStream");


        // 从元素读取数据
        DataStreamSource<Event> streamFromElement = env.fromElements(
                new Event("spx", "./home", 2000L),
                new Event("ttt", "./prod", 3000L)
        );
        streamFromElement.print("streamFromElement");


        // 从 socket 文本流中读取
        DataStreamSource<String> socketStream = env.socketTextStream("hadoop102", 7777);


        env.execute();

    }

}
